*** Do-file to replicate only:
*** Robustness tests incorporating uncertainty around expert-coded observed level of democracy using the Varieties of Democracy posteriors data.
*** Manuscript: Time for a Haircut: Political Regimes and Sovereign Debt Restructurings
*** Author: Ignacio Mamone 
*** Journal: International Interactions
*** Date: March 3, 2020
*** Analysis run on StataSE 16
*** The annotated code builds on Bizarro, Fernando, Coppedge, Michael, and Daniel Pemstein. 2016. "Incorporating V-DEM's Uncertainty Estimates in Regression Analysis." Unpublished Manuscript, Varieties of Democracy Project.


*****************************************************
* Event History Analysis with Uncertainty Parameters
*****************************************************
clear
cd
use "time_for_a_haircut.dta"

** Merge the main dataset "time_for_a_haircut" with a cleaned version of the Varieties of Democracy Project's "Posteriors" Dataset stored in the University of Notre Dame's CurateND Archive data repository. Each row contains 900 uncertainty estimates from the V-DEM measurement model for each country-year.
** Note: the latest available collection of posteriors from V-Dem is from 2016.
merge 1:1 cow year using "vdem_posteriors.dta", generate(_merge_posteriors)
drop if _merge_posteriors==2

** Estimate baseline Cox survival model (equivalent to Model 1, Table 1 but with continuous treatment as in Model 2, Table S.3) on original dataset but restricted to years prior to 2016 to match posteriors data availability.
stcox v2x_polyarchy cbi debt_gdp gdppclog exports fixedrate resourcerents bond_restructuring stratanew if year<2016, nohr

** Replicate Cox survival model of the effect of level of democracy on duration of restructuring episodes incorporating the uncertainty from expert-coded data using the posterior distribution from the V-Dem measurement model.
** Notes: it may take several minutes (around 5 to 10 minutes) to run.
** Notes: Estimates from MCMC using the posteriors uncertainty parameters with selection-correction parametric duration models yield similar results as those reported in Model 2, Table 1, but I focus on the Cox survival model and its estimation with uncertainty parameters to ease interpretation (as most readers in political science are familiarized with Cox models) and reduce significantly the time of computation.

*Run Monte Carlo Markov Chain
forvalues i = 2/901 {
display `i'
*Fit the model, using the ith draw from the posterior
quietly stcox v2x_polyarchy_v`i' cbi debt_gdp gdppclog exports fixedrate resourcerents bond_restructuring stratanew, nohr
*Extract the coefficients and variance-covariance matrix
matrix b = e(b)
matrix V = e(V)
local blength = colsof(b)
*Preserve the dataset, take 10 normal draws from the posterior distribution, and restore the dataset.
*Catch possible errors in drawnorm and drop these iterations.
preserve
capture quietly drawnorm b1-b`blength', double n(10) means(b) cov(V) clear
if _rc==0 {
mkmat b1-b`blength', matrix(bsample)
matrix posterior = nullmat(posterior) \ bsample
}
else {
display "Error drawing sample...iteration dropped"
}
restore

}

*Get posterior ready to work with
svmat posterior
*Calculate means and standard deviations. The "posterior1" outcome listed in the table is the coefficient of interest for this robustness test.
tabstat posterior*, stat(mean sd)

*Find the bounds of the 95% credible interval. The "posterior1" outcome listed in the table is the coefficient of interest for this robustness test.
centile posterior*, centile(2.5, 97.5)


drop posterior*
clear


*****************************************************
* Regression Analysis with Uncertainty Parameters
*****************************************************
clear
cd
use "time_for_a_haircut.dta"

** Merge the main dataset with the Varieties of Democracy Project's 900 Posteriors Dataset from the Measurement Model
** Note: the latest available collection of posteriors from V-Dem is from 2016.
merge 1:1 cow year using "vdem_posteriors.dta", generate(_merge_posteriors)
drop if _merge_posteriors==2

** Estimate baseline double-hurdle model of the effect of level of democracy on size of haircuts (equivalent to Model 2, Table S.5) but restricted to years prior to 2016 to match posteriors data availability.
** Note: Requires craggit program as explained in the README.txt of the supplementary materials folder.
gen haircutbounded = haircut
replace haircutbounded=0.1 if haircut<=0
gen haircutboundednew = haircutbounded/100
replace haircutboundednew=0 if haircutbounded==.
label var haircutboundednew "Haircut (% creditor loses)"
craggit rescase v2x_polyarchy cbi debt_gdp gdppclog exports fixedrate resourcerents stratanew us10yrrate if year<2016, ///
 second(haircutboundednew v2x_polyarchy cbi debt_gdp gdppclog exports fixedrate resourcerents stratanew ) vce(cluster cow)

** Replicate double-hurdle model incorporating the uncertainty from expert-coded data using the posterior distribution from the V-Dem measurement model.
** Note: it may take several minutes (around 60 mins.) to run.
*Run Monte Carlo Markov Chain
forvalues i = 2/901 {
display `i'
*Fit the model, using the ith draw from the posterior
quietly craggit rescase v2x_polyarchy_v`i' cbi debt_gdp gdppclog exports fixedrate resourcerents stratanew us10yrrate, ///
 second(haircutboundednew v2x_polyarchy cbi debt_gdp gdppclog exports fixedrate resourcerents stratanew ) vce(cluster cow)
*Extract the coefficients and variance-covariance matrix
matrix b = e(b)
matrix V = e(V)
local blength = colsof(b)
*Preserve the dataset, take 10 normal draws from the posterior distribution, and restore the dataset.
*Catch possible errors in drawnorm and drop these iterations.
preserve
capture quietly drawnorm b1-b`blength', double n(10) means(b) cov(V) clear
if _rc==0 {
mkmat b1-b`blength', matrix(bsample)
matrix posterior = nullmat(posterior) \ bsample
}
else {
display "Error drawing sample...iteration dropped"
}
restore

}

*Get posterior ready to work with
svmat posterior
*Calculate means and standard deviations. The "posterior11" outcome listed in the table is the coefficient of interest for this robustness test (level of democracy is the first covariate in the second tier in the double-hurdle model which includes a total of 20 covariates or posteriors in this table)
tabstat posterior*, stat(mean sd)

*Find the bounds of the 95% credible interval. The "posterior11" outcome listed in the table is the coefficient of interest for this robustness test (level of democracy is the first covariate in the second tier in the double-hurdle model which includes a total of 20 covariates or posteriors in this table).
centile posterior*, centile(2.5, 97.5)

drop posterior*
clear